package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> selectAll() {
        List<Department> departmentList = departmentMapper.selectAll();
        return departmentList;
    }

    @Override
    public PageInfo<Department> selectByQo(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> list = departmentMapper.selectAll();
        PageInfo<Department> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteBydeptId(Long deptId) {
        departmentMapper.deleteByPrimaryKey(deptId);
    }

    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId()==null){
            departmentMapper.insert(department);
        }else {
            departmentMapper.updateByPrimaryKey(department);
        }
    }

    @Override
    public Department selectBydeptId(Long deptId) {
        Department department = departmentMapper.selectByPrimaryKey(deptId);
        return department;
    }
}
